<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="正则,">










<meta name="description" content="&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 正则表达式(regular expression)描述了一种字符串匹配的模式（pattern），可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。">
<meta name="keywords" content="正则">
<meta property="og:type" content="article">
<meta property="og:title" content="正则表达式">
<meta property="og:url" content="https://shellingfordly.gitee.io/2019/10/09/正则表达式/index.html">
<meta property="og:site_name" content="shelllingfordly">
<meta property="og:description" content="&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 正则表达式(regular expression)描述了一种字符串匹配的模式（pattern），可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-09T09:09:01.421Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="正则表达式">
<meta name="twitter:description" content="&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 正则表达式(regular expression)描述了一种字符串匹配的模式（pattern），可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://shellingfordly.gitee.io/2019/10/09/正则表达式/">





  <title>正则表达式 | shelllingfordly</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">shelllingfordly</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">人生本就是一场孤独的旅行</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-首页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-归档">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-分类">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-标签">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-评论区">
          <a href="/comment" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-comment"></i> <br>
            
            评论区
          </a>
        </li>
      
        
        <li class="menu-item menu-item-关于我">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于我
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://shellingfordly.gitee.io/2019/10/09/正则表达式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="shellingford ly">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/uploads/images/header.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shelllingfordly">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">正则表达式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-09T17:06:12+08:00">
                2019-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/正则/" itemprop="url" rel="index">
                    <span itemprop="name">正则</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/09/正则表达式/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/09/正则表达式/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <span>
&nbsp;&nbsp;&nbsp;&nbsp;
正则表达式(regular expression)描述了一种字符串匹配的模式（pattern），可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

</span>

<a id="more"></a>

<h2 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h2><ul>
<li>正则是一个对象</li>
</ul>
<h3 id="规则"><a href="#规则" class="headerlink" title="规则"></a>规则</h3><ul>
<li>字面量方式 //</li>
<li>构造函数</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> reg = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"字符串"</span>)</span><br></pre></td></tr></table></figure>

<h3 id="元字符"><a href="#元字符" class="headerlink" title="元字符"></a>元字符</h3><p>在正则中有特殊意义</p>
<h4 id="d和-D"><a href="#d和-D" class="headerlink" title="\d和\D"></a>\d和\D</h4><ul>
<li>\d 匹配数字</li>
<li>\D 匹配非数字</li>
</ul>
<h4 id="s和-S"><a href="#s和-S" class="headerlink" title="\s和\S"></a>\s和\S</h4><ul>
<li>\s 空格</li>
<li>\S 非空格</li>
</ul>
<h4 id="w和-W"><a href="#w和-W" class="headerlink" title="\w和\W"></a>\w和\W</h4><ul>
<li>\w 数字、字符、下划线_</li>
<li>\W 除\w之外的所有字符</li>
</ul>
<h4 id="b"><a href="#b" class="headerlink" title="\b"></a>\b</h4><p>独立部分/单词边界</p>
<blockquote>
<p>指的是匹配到一个完整的独立的单词，其前后都没有多余的东西；除\w匹配的数字、字符、下划线之外的所有字符都可以被认为是单词边界(,;.中文等)，包括起始位置和终止位置</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"hhelloo 1hello,hello_ 中文hello."</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\bmy\b/</span></span><br><span class="line"><span class="comment">// 将会匹配到最后一个hello</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg));</span><br></pre></td></tr></table></figure>

<h4 id><a href="#" class="headerlink" title="."></a>.</h4><p>匹配任意字符，不包括 <strong>\n</strong>和 <strong>\r</strong></p>
<h4 id="-1"><a href="#-1" class="headerlink" title="\"></a>\</h4><p>将有意义的字符转换为普通字符；对正则中所有有意义的字符的匹配时都必须使用“\”转义</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abc . def'</span></span><br><span class="line"><span class="comment">// 直接写 /./ 将匹配到a</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\./</span></span><br><span class="line"><span class="comment">// 返回 .</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg));</span><br></pre></td></tr></table></figure>

<h4 id="r"><a href="#r" class="headerlink" title="\r"></a>\r</h4><p>制表符，基本看不到</p>
<h4 id="量词-数量"><a href="#量词-数量" class="headerlink" title="量词/数量"></a>量词/数量</h4><p>匹配相同规则连续出现n词的字符</p>
<ul>
<li>只对紧跟着的前面的规则连续匹配</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'12345 67890'</span></span><br><span class="line"><span class="comment">// 匹配3个数字</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\d&#123;3&#125;/</span></span><br></pre></td></tr></table></figure>

<p>规则</p>
<ul>
<li>{n} n个</li>
<li>{n,m} 最少n个，最多m个</li>
<li>{n,} 最少n个，最多无穷个</li>
</ul>
<p>简写</p>
<ul>
<li>{0,} === * 表示0到正无穷个</li>
<li>{1,} === + 表示1到正无穷个</li>
<li>{0,1} === ? 表示要么有1个，要么没有</li>
</ul>
<h4 id="贪婪匹配"><a href="#贪婪匹配" class="headerlink" title="贪婪匹配"></a>贪婪匹配</h4><p>量词默认使用贪婪模式匹配，从最大值开始匹配。若满足最大值则递减直到最小值，否则匹配失败</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'12345 67890'</span></span><br><span class="line"><span class="comment">// 匹配5个数字</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\d&#123;2,10&#125;/</span></span><br></pre></td></tr></table></figure>

<h4 id="非贪婪-懒惰匹配"><a href="#非贪婪-懒惰匹配" class="headerlink" title="非贪婪/懒惰匹配"></a>非贪婪/懒惰匹配</h4><p>在量词后加上 <strong>?</strong> 则表示使用懒惰模式匹配</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'12345 67890'</span></span><br><span class="line"><span class="comment">// 匹配2个数字</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\d&#123;2,10&#125;?/</span></span><br></pre></td></tr></table></figure>

<h3 id="修饰符-标识符"><a href="#修饰符-标识符" class="headerlink" title="修饰符/标识符"></a>修饰符/标识符</h3><h4 id="g"><a href="#g" class="headerlink" title="g"></a>g</h4><p>golbal全局匹配，匹配整个字符串中所有符合正则规则的全部字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'1231adad124314cszf45'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\d+/g</span></span><br><span class="line"><span class="comment">// 返回 [ '1231', '124314', '45' ]</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg))</span><br></pre></td></tr></table></figure>

<h4 id="i"><a href="#i" class="headerlink" title="i"></a>i</h4><p>忽略大小写</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abc'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/ABC/gi</span></span><br><span class="line"><span class="comment">// 返回 ["abc"]</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg))</span><br></pre></td></tr></table></figure>

<h4 id="m"><a href="#m" class="headerlink" title="m"></a>m</h4><p>换行匹配</p>
<h4 id="组-子项"><a href="#组-子项" class="headerlink" title="()组/子项"></a>()组/子项</h4><p>将()内的字符串作为一个整体来匹配</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abccccabc'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/abc+/g</span></span><br><span class="line"><span class="comment">// 此时得到的结果为 [ 'abcccc', 'abc' ]</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg))</span><br><span class="line"><span class="comment">// 此时得到的结果为 [ 'abc', 'abc' ]</span></span><br><span class="line">reg = <span class="regexp">/(abc)+/g</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg))</span><br></pre></td></tr></table></figure>

<ul>
<li>\1 表示第一个子集匹配的内容</li>
<li>\2 表示第二个子集匹配的内容</li>
<li>以此类推</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abcabcbc'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/(abc)\1/g</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg)); <span class="comment">// [ 'abcabc' ]</span></span><br><span class="line">reg = <span class="regexp">/(aa)|(bc)\2/</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg)); <span class="comment">// [ 'bcbc' ]</span></span><br></pre></td></tr></table></figure>

<h4 id="-2"><a href="#-2" class="headerlink" title="^"></a>^</h4><p>匹配以某字符串为起始的字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abccccabc'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/^abc/g</span></span><br></pre></td></tr></table></figure>

<h4 id="-3"><a href="#-3" class="headerlink" title="$"></a>$</h4><p>匹配以某字符串为结束的字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abccccabc'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/^abc$/g</span></span><br></pre></td></tr></table></figure>

<h4 id="字符集"><a href="#字符集" class="headerlink" title="[]字符集"></a>[]字符集</h4><p>匹配符合区间内的字符</p>
<ol>
<li>字段：返回两个字符的Unicode码之间的所有对应的字符</li>
<li>在字符集中的<code>(){}//?+*-</code>等有特殊意义的字符都会失去其意义，变为普通字符</li>
</ol>
<ul>
<li>[1-9] [a-z]</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abcdefgabcd'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/[a-c]/g</span></span><br><span class="line"><span class="comment">// [ '1', '2', '3' ]</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg))</span><br></pre></td></tr></table></figure>

<ul>
<li>[123] 表示或者，1或者2或者3</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'123456789'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/[123]/</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg)); <span class="comment">//[ '1' ]</span></span><br><span class="line">reg = <span class="regexp">/[123]/g</span> <span class="comment">// 匹配全部</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg)); <span class="comment">//[ '1', '2', '3' ]</span></span><br></pre></td></tr></table></figure>

<ul>
<li>匹配中文的Unicode码：[\u4e00-\u9fa5]</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'中abcdefgabcd文'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/[\u4e00-\u9fa5]/g</span></span><br></pre></td></tr></table></figure>

<ul>
<li>在[]字符集中^有特殊含义，表示匹配除了^后的字符的所有字符</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'123a'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/[^12]/g</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg)); <span class="comment">// [ '3', 'a' ]</span></span><br></pre></td></tr></table></figure>

<h4 id="str1-str2"><a href="#str1-str2" class="headerlink" title="str1 | str2"></a>str1 | str2</h4><p>或者</p>
<ul>
<li>匹配时<code>|</code>前后的字符串将被视为一个整体</li>
<li>若找到str1则返回匹配到的str1；若没有则寻找str2</li>
<li>若加上g全局匹配，则匹配到str1和str2</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abcdefgabcd'</span></span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/a|b/</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg)); <span class="comment">// 匹配到 a</span></span><br></pre></td></tr></table></figure>

<h3 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h3><h4 id="reg-test-str"><a href="#reg-test-str" class="headerlink" title="reg.test(str)"></a>reg.test(str)</h4><p>检测str字符串中是否有符合正则匹配规则的字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"这是一个字符串"</span>;</span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/字符串/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(str)); <span class="comment">//值为true</span></span><br></pre></td></tr></table></figure>

<h4 id="str-match-reg"><a href="#str-match-reg" class="headerlink" title="str.match(reg)"></a>str.match(reg)</h4><p>返回匹配规则的字符串数组</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/weixin.jpg" alt="shellingford ly 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/zhifubao.jpg" alt="shellingford ly 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/正则/" rel="tag"># 正则</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/10/04/HTML5/" rel="next" title="HTML5">
                <i class="fa fa-chevron-left"></i> HTML5
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/images/header.jpg" alt="shellingford ly">
            
              <p class="site-author-name" itemprop="name">shellingford ly</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/shellingfordly" target="_blank" title="github">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/shellingfordly" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-maxcdn"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/shellingfordly" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.cnblogs.com/shellingfordly" target="_blank" title="博客园">
                      
                        <i class="fa fa-fw fa-rss"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:shellingfordly@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://space.bilibili.com/288333302" target="_blank" title="bilibili">
                      
                        <i class="fa fa-fw fa-globe"></i></a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#正则表达式"><span class="nav-number">1.</span> <span class="nav-text">正则表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#规则"><span class="nav-number">1.1.</span> <span class="nav-text">规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#元字符"><span class="nav-number">1.2.</span> <span class="nav-text">元字符</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#d和-D"><span class="nav-number">1.2.1.</span> <span class="nav-text">\d和\D</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#s和-S"><span class="nav-number">1.2.2.</span> <span class="nav-text">\s和\S</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#w和-W"><span class="nav-number">1.2.3.</span> <span class="nav-text">\w和\W</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#b"><span class="nav-number">1.2.4.</span> <span class="nav-text">\b</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#null"><span class="nav-number">1.2.5.</span> <span class="nav-text">.</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-1"><span class="nav-number">1.2.6.</span> <span class="nav-text">\</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#r"><span class="nav-number">1.2.7.</span> <span class="nav-text">\r</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#量词-数量"><span class="nav-number">1.2.8.</span> <span class="nav-text">量词/数量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#贪婪匹配"><span class="nav-number">1.2.9.</span> <span class="nav-text">贪婪匹配</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#非贪婪-懒惰匹配"><span class="nav-number">1.2.10.</span> <span class="nav-text">非贪婪/懒惰匹配</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修饰符-标识符"><span class="nav-number">1.3.</span> <span class="nav-text">修饰符/标识符</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#g"><span class="nav-number">1.3.1.</span> <span class="nav-text">g</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#i"><span class="nav-number">1.3.2.</span> <span class="nav-text">i</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#m"><span class="nav-number">1.3.3.</span> <span class="nav-text">m</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#组-子项"><span class="nav-number">1.3.4.</span> <span class="nav-text">()组/子项</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-2"><span class="nav-number">1.3.5.</span> <span class="nav-text">^</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-3"><span class="nav-number">1.3.6.</span> <span class="nav-text">$</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#字符集"><span class="nav-number">1.3.7.</span> <span class="nav-text">[]字符集</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#str1-str2"><span class="nav-number">1.3.8.</span> <span class="nav-text">str1 | str2</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#方法"><span class="nav-number">1.4.</span> <span class="nav-text">方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#reg-test-str"><span class="nav-number">1.4.1.</span> <span class="nav-text">reg.test(str)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#str-match-reg"><span class="nav-number">1.4.2.</span> <span class="nav-text">str.match(reg)</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shellingford ly</span>

  
</div>


  <div class="powered-by">
  <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a>
  </div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info"> 
  <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">
  NexT</a>
  </div>




<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">33.7k</span>
</div>

        
<div class="busuanzi-count">
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'Cs5UqCDDJC5JezlK5wuzf2ql-gzGzoHsz',
        appKey: 'TC9fgcijHIjkjEwwCz6aQSJi',
        placeholder: '说点什么呢...',
        avatar:'monsterid',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,QQZone,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,QQZone,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  

  

  

</body>
</html>
